package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonPointAccount;
import me.firstapp.common.module.bbs.JsonPointAccountTrace;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.PointService;

@Service
public class PointServiceImpl extends BaseServiceImpl implements PointService {

	@Override
	public SingleObject<JsonPointAccount> findPointAccount(Long memberId) {
		SingleObject<JsonPointAccount> result = new SingleObject<JsonPointAccount>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			JSONObject resultObject = api(parameters, "/point/findMemberPoint.do", HttpMethod.GET, "查询用户积分");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject _jsonObject = resultObject.getJSONObject("object");
				if (_jsonObject != null) {
					JsonPointAccount account = JSONObject.toJavaObject(_jsonObject, JsonPointAccount.class);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setObject(account);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonPointAccountTrace> findPointAccountTraces(Integer pageNo, Integer pageSize, Long memberId) {
		PageObject<JsonPointAccountTrace> result = new PageObject<JsonPointAccountTrace>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("memberId", memberId);

			JSONObject resultObject = api(parameters, "/point/findPointAccountTrace.do", HttpMethod.GET, "分页查询积分变动记录");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonPointAccountTrace> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonPointAccountTrace> traceList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonPointAccountTrace trace = JSONObject.toJavaObject(_jsonObject,
										JsonPointAccountTrace.class);
								traceList.add(trace);
							}
						}
					}
					jsonPage.setList(traceList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public SingleObject<Object> alterPayPassword(Long memberId, String oldPassword, String newPassword, String code) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("oldPassword", oldPassword);
			parameters.put("newPassword", newPassword);
			parameters.put("code", code);

			JSONObject resultObject = api(parameters, "/point/alterPayPassword.do", HttpMethod.POST, "修改支付密码");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public SingleObject<JsonPointAccount> rechargePoint(Long memberId, String cardNo, String password,
			String payPassword, String code) {
		SingleObject<JsonPointAccount> result = new SingleObject<JsonPointAccount>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("cardNo", cardNo);
			parameters.put("password", password);
			parameters.put("payPassword", payPassword);
			parameters.put("code", code);
			JSONObject resultObject = api(parameters, "/point/rechargePoint.do", HttpMethod.POST, "积分充值");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject _jsonObject = resultObject.getJSONObject("object");
				if (_jsonObject != null) {
					JsonPointAccount account = JSONObject.toJavaObject(_jsonObject, JsonPointAccount.class);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setObject(account);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonPointAccountTrace> findPointAccountTracePageByParams(Long memberId, Integer pageNo,
			Integer pageSize, Map<String, Object> searchParams, String sortParam, String sortType, Long startTime,
			Long endTime) {
		PageObject<JsonPointAccountTrace> result = new PageObject<JsonPointAccountTrace>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			parameters.put("startTime", startTime);
			parameters.put("endTime", endTime);
			parameters.put("memberId", memberId);
			JSONObject jsonObject = apiBody(parameters, "/point/findPointTracePageByParams.do", "分页查询积分流水",
					searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonPointAccountTrace> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonPointAccountTrace> traceList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonPointAccountTrace _trace = JSONObject.toJavaObject(_jsonObject,
										JsonPointAccountTrace.class);
								traceList.add(_trace);
							}
						}
					}
					jsonPage.setList(traceList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}
}
